package com.hw.one.collect.ocec.util;

import com.hw.one.collect.config.ConditionConfig;
import com.hw.one.collect.util.DateUtil;

import java.io.File;
import java.io.FilenameFilter;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Calendar;

/**获取ocec有机碳 文件路径
 * @Class OcecUtil
 * @Author guoshenzhen
 * @Date 2018/3/21  11:10
 */
public class OcecUtil {
    public static final String getLatestFilePath(String YYYYMMDD_TIME){
        String filePath = ConditionConfig.get("filePath", ConditionConfig.ocec);
        File file = new File(filePath);
        int hourConstant =   Calendar.HOUR_OF_DAY;
        Calendar cal = Calendar.getInstance();
        cal.add(hourConstant, -1);
        int hour = cal.get(hourConstant);
        YYYYMMDD_TIME = null != YYYYMMDD_TIME ? YYYYMMDD_TIME : DateUtil.parseDate(cal.getTime(), DateUtil.NO_SPLIT_SDF_YMD);
        String fileNameSufferfix = MessageFormat.format("SLI_OCEC_{0}", YYYYMMDD_TIME);
        String[] list = file.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if(name.startsWith(fileNameSufferfix) && name.endsWith("_Res.csv")){
                    return true;
                }
                return false;
            }
        });
        if(null != list && list.length > 0){
            Arrays.sort(list);
            String fileName = list[list.length - 1];
            filePath = filePath + File.separator + fileName;
            return filePath;
        }
        return null;
    }


}
